package cn.uniqueinfo.dp.util;

import cn.uniqueinfo.dp.vo.MenuVO;

import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-02-19 17:05:21
 * @description:tree菜单
 */
public class TreeMenuUtil {

    public static String getJson(List<MenuVO> list) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("[");
        iterGet(list, 0L, buffer);
        buffer.append("]");
        String tmp = buffer.toString();
        tmp = tmp.replaceAll(",\n]", "\n]");
        return tmp;
    }

    private static void iterGet(List<MenuVO> list, Long pid, StringBuffer buffer) {
        for (MenuVO node : list) {
            // 查找所有父节点为pid的所有对象，然后拼接为json格式的数据
            if (pid.equals(node.getIpid())) {
                buffer.append(
                        "{'id':'"
                                + node.getId()
                                + "','text':'"
                                + node.getMenuName()
                                + "','iconCls':'"
                                + node.getMenuIcon()
                                + "','url':'"
                                + node.getMenuPath()
                                + "','children':[");
                // 递归
                iterGet(list, node.getId(), buffer);
                buffer.append("]},\n");
            }
        }
    }
}
